class Solution {
    /**
        思路：DFS+模拟乘法
    */
    int ans = 0;
    public int sumNumbers(TreeNode root) {
        dfs(root,0);
        return ans;
    }
    public void dfs(TreeNode root,int sum){
        if( root==null )
            return;
        sum = sum*10+root.val;
        if( root.left==null && root.right==null ){
            ans += sum;
        }
        dfs(root.left,sum);
        dfs(root.right,sum);
    }
}

class Solution {
    /*
        思路：StringBuild拼接，需要注意的是引用类型，如果字符串append拼接了，必须要delete
    */
    int ans = 0;
    StringBuilder sum = new StringBuilder();
    public int sumNumbers(TreeNode root) {
        dfs(root);
        return ans;
    }
    public void dfs(TreeNode root){
        if( root==null )
            return;
        sum.append(root.val);
        if( root.left==null && root.right==null ){
            ans += Integer.parseInt(sum.toString());
        }
        dfs(root.left);
        dfs(root.right);
        sum.delete(sum.length()-1,sum.length());
    }
}